/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package TreeView;

/**
 *
 * @author Mr.Sam
 */
import java.awt.*;
import java.io.Serializable;
import java.lang.IllegalArgumentException;

/**
* A layout manager to display the component of the container laid by the
* instance vertically.
*
* @author © 1999 DIRAT Laurent
* @version 1.0  01/06/99
*/
public class OneColumnLayout implements LayoutManager2, Serializable {
    // Methods of the LayoutManager interface
    
    /**
     * Adds the specified component with the specified name to
     * the layout.
     * @param name the component name
     * @param comp the component to be added
     */
    public void addLayoutComponent(String name, Component comp) {
    }

    /**
     * Removes the specified component from the layout.
     * @param comp the component ot be removed
     */
    public void removeLayoutComponent(Component comp) {
    }

    /** 
     * Calculates the minimum size dimensions for the specified 
     * panel given the components in the specified parent container.
     * @param parent the component to be laid out
     * @see #preferredLayoutSize
     */
    public Dimension minimumLayoutSize(Container parent) {
        return preferredLayoutSize(parent);
    }

    // Methods of the LayoutManager2 interface

    /**
     * Adds the specified component to the layout, using the specified
     * constraint object.
     * @param comp the component to be added
     * @param constraints  where/how the component is added to the layout.
     */
    public void addLayoutComponent(Component comp, Object constraints) {
    }

    /** 
     * Returns the maximum size of this component.
     * @see java.awt.Component#getMinimumSize()
     * @see java.awt.Component#getPreferredSize()
     * @see LayoutManager
     */
    public Dimension maximumLayoutSize(Container target) {
        return preferredLayoutSize(target);
    }

    /**
     * Returns the alignment along the x axis.  This specifies how
     * the component would like to be aligned relative to other 
     * components.  The value should be a number between 0 and 1
     * where 0 represents alignment along the origin, 1 is aligned
     * the furthest away from the origin, 0.5 is centered, etc.
     */
    public float getLayoutAlignmentX(Container target) {
        return 0.0f;
    }

    /**
     * Returns the alignment along the y axis.  This specifies how
     * the component would like to be aligned relative to other 
     * components.  The value should be a number between 0 and 1
     * where 0 represents alignment along the origin, 1 is aligned
     * the furthest away from the origin, 0.5 is centered, etc.
     */
    public float getLayoutAlignmentY(Container target) {
        return 0.0f;
    }

    /**
     * Invalidates the layout, indicating that if the layout manager
     * has cached information it should be discarded.
     */
    public void invalidateLayout(Container target) {
    }
    
    /**
     * Calculates the preferred size dimensions for the specified 
     * panel given the components in the specified parent container.
     * @param parent the component to be laid out
     *  
     * @see #minimumLayoutSize
     */
    public Dimension preferredLayoutSize(Container parent) {
        int width = 0;
        int height = 0;
        Component tmp;
        Dimension size;
        
        for (int i = 0; i < parent.getComponentCount(); i++ ) {
            tmp = parent.getComponent(i);
            size = tmp.getPreferredSize();
            tmp.setSize(size);
            
            width = (int) Math.max(size.width, width);
            height += size.height;
        }
        
        Component father = parent.getParent();
        width = (int) Math.max(width, (father.getSize()).width - 5);
        
        for (int i = 0; i < parent.getComponentCount(); i++ ) {
            tmp = parent.getComponent(i);
            size = tmp.getSize();
            tmp.setSize(width, size.height);
        }
        
        return new Dimension(width, height);
    }
        
    /*
     * Lays out the container in the specified panel.
     * @param parent the component which needs to be laid out 
     */
     public void layoutContainer(Container parent) {
        Component current, previous = null;
        Rectangle bounds;
        
        for (int i = 0; i < parent.getComponentCount(); i++) {
            current = parent.getComponent(i);
            
            if (i == 0 )
                current.setLocation(0, 0);
            else {
                bounds = previous.getBounds();
                current.setLocation(0, bounds.y + bounds.height);
            }
            
            previous = current;
        }
        
     }
}


